/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sysmo.server.action;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.upload.FormFile;
import org.sysmo.server.SysmoServer;
import org.sysmo.server.node.ConnectedNode;
import org.sysmo.server.node.item.ConnectedItem;
import org.sysmo.server.node.item.ItemExistsException;
import org.sysmo.server.node.item.ItemScript;
import org.sysmo.server.node.item.WarningContect;

/**
 *
 * @author Derek
 */
public class DoEditItemAction extends org.apache.struts.action.Action {
    
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        SysmoServer server = (SysmoServer)getServlet().getServletContext().getAttribute("sysmoServer");
        DynaActionForm dynaForm =(DynaActionForm)form;
        int iid = (Integer)dynaForm.get("iid");
        String input = "/Node.do?nid=" + request.getParameter("nid");
        ConnectedItem item;
        boolean isNew;
        if (iid == 0) {
            ConnectedNode node = (ConnectedNode)server.getNode(Integer.parseInt(request.getParameter("nid")));
            try {
                item = (ConnectedItem)node.addItem(dynaForm.getString("name"));
            } catch (ItemExistsException e) {
                request.setAttribute("error", "监控项名称已存在");
                request.setAttribute("showBox", "1");
                return new ActionForward(input);
            }
            isNew = true;
        } else {
            item = (ConnectedItem)server.getItem(iid);
            try {
                item.setName(dynaForm.getString("name"));
            } catch (ItemExistsException e) {
                request.setAttribute("error", "监控项名称已存在");
                request.setAttribute("editItem", item);
                return new ActionForward(input);
            }
            isNew = false;
            item.pauseDataSending();
        }

        //设置采集数据脚本参数
        ItemScript script = item.getMonitorScript();
        script.setCommandLine(dynaForm.getString("commandLine"));

        FormFile file = (FormFile)dynaForm.get("cscript");

        if (!file.getFileName().equals("")) {
            int postPos = file.getFileName().lastIndexOf(".");
            String postfix = "";
            if (postPos >= 0) {
                postfix = file.getFileName().substring(postPos + 1);
            }
            script.setPostfix(postfix);

            //发送采集数据脚本
            InputStream in = file.getInputStream();
            ByteArrayOutputStream data = new ByteArrayOutputStream();
            int b;
            while ((b = in.read()) != -1) {
                data.write(b);
            }
            data.close();
            script.sendScriptToClient(data.toByteArray());
        } else {
            if (isNew) {
                script.setPostfix("sh");
                script.sendScriptToClient(new byte[0]);
            } else {
                script.sendSettingsToClient();
            }
        }

        item.setRunInterval((Integer)dynaForm.get("timeInterval"));
        item.setValidateScript(dynaForm.getString("vscript"));

        //设置警报目的邮件地址
        WarningContect warning = item.getWarningContect();
        warning.setEmail(dynaForm.getString("warningEmail"));
        item.setWarningContect(warning);

        item.sync();

        item.resumeDataSending();

        request.removeAttribute("showBox");
        request.removeAttribute("error");
        if (isNew) {
            return new ActionForward("/Item.do?iid=" + item.getID(), true);
        } else {
            return new ActionForward(input, true);
        }
    }
}
